//
//  NSObject+GetValue.m
//  ProjectDemo
//
//  Created by IOS3 on 2018/10/22.
//  Copyright © 2018 IOS3. All rights reserved.
//

#import "NSObject+GetValue.h"

@implementation NSObject (GetValue)


- (NSObject *(^)(NSInteger))valueWithIndex {
    __weak typeof(self)weakSelf = self;
    return ^NSObject *(NSInteger index) {
        __strong typeof(weakSelf)strongSelf = weakSelf;
        if ([strongSelf isKindOfClass:[NSArray class]]) {
            NSArray *arr = (NSArray *)strongSelf;
            if (arr.count > index) {
                return arr[index];
            }
        }
        return nil;
    };
}

- (NSObject *(^)(NSString *))valueWithKey {
    __weak typeof(self)weakSelf = self;
    return ^NSObject *(NSString *key) {
        __strong typeof(weakSelf)strongSelf = weakSelf;
        if ([strongSelf isKindOfClass:[NSDictionary class]]) {
            NSDictionary *dict = (NSDictionary *)strongSelf;
            id value = dict[key];
            if ([value isKindOfClass:[NSNull class]]) {
                return nil;
            }
            return value;
        }
        return nil;
    };
}

- (BOOL)isEmpty {
    if (self == nil) {
        return true;
    }
    if ([self isKindOfClass:[NSNull class]]) {
        return true;
    }
    if ([self respondsToSelector:@selector(length)]) {
        if ([(NSData *)self length] == 0) {
            return true;
        }
    }
    if ([self respondsToSelector:@selector(count)]) {
        if ([(NSArray *)self count] == 0) {
            return true;
        }
    }
    return false;
}

@end
